package com.admin.education.services.Implements;

import com.admin.education.common.exceptions.ResourceNotFoundException;
import com.admin.education.dataTransferObject.PageResponse;
import com.admin.education.dataTransferObject.ServicesExcuteResult;
import com.admin.education.dataTransferObject.subject.Mapper.SubjectMapper;
import com.admin.education.dataTransferObject.subject.Request.CreateSubjectRequest;
import com.admin.education.dataTransferObject.subject.Request.QuerySubjectRequest;
import com.admin.education.dataTransferObject.subject.Request.UpdateSubjectRequest;
import com.admin.education.dataTransferObject.subject.SubjectTransfer;
import com.admin.education.models.QSubjects;
import com.admin.education.models.Subjects;
import com.admin.education.repositories.SubjectsRepository;
import com.admin.education.services.Interfaces.ISubjectService;
import com.querydsl.jpa.impl.JPAQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;

import javax.transaction.Transactional;
import javax.validation.Valid;
import java.util.List;

@Service
@Transactional
@Validated
public class SubjectService implements ISubjectService {
    @Autowired
    private SubjectMapper subjectMapper;
    private QSubjects qSubjects;
    @Autowired
    private SubjectsRepository subjectsRepository;
    public  SubjectService(){
        qSubjects = QSubjects.subjects;
    }
    @Override
    public ServicesExcuteResult createSubject(@Valid CreateSubjectRequest createSubjectRequest) {
        ServicesExcuteResult servicesExcuteResult= new ServicesExcuteResult();

        Subjects subjects =subjectMapper.mapFrom(createSubjectRequest);

        this.subjectsRepository.persist(subjects);
        servicesExcuteResult.setSuccess(true);
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult<SubjectTransfer> querySubjectById(long subjectId) throws ResourceNotFoundException {
        ServicesExcuteResult<SubjectTransfer> servicesExcuteResult = new ServicesExcuteResult<>();

        JPAQuery<Subjects> subjectsJPAQuery = this.subjectsRepository.selectFrom(qSubjects).where(qSubjects.id.eq(subjectId));
        if(0!=subjectsJPAQuery.fetchCount()){
            Subjects subjects = subjectsJPAQuery.fetchFirst();

            servicesExcuteResult.setData(subjectMapper.mapFrom(subjects));
            servicesExcuteResult.setSuccess(true);
        }
        else{
            throw new ResourceNotFoundException("科目不存在");
        }
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult updateSubject(long subjectId, @Valid UpdateSubjectRequest updateSubjectRequest) throws ResourceNotFoundException {
        ServicesExcuteResult<SubjectTransfer> servicesExcuteResult = new ServicesExcuteResult<>();

        JPAQuery<Subjects> subjectsJPAQuery = this.subjectsRepository.selectFrom(qSubjects).where(qSubjects.id.eq(subjectId));
        if(0!=subjectsJPAQuery.fetchCount()){

            Subjects subjects = subjectsJPAQuery.fetchFirst();

            subjects = subjectMapper.mapFrom(updateSubjectRequest,subjects);

            this.subjectsRepository.merge(subjects);
            servicesExcuteResult.setSuccess(true);
        }
        else{
            throw new ResourceNotFoundException("科目不存在");
        }
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult deleteSubject(long subjectId) throws ResourceNotFoundException {
        ServicesExcuteResult<SubjectTransfer> servicesExcuteResult = new ServicesExcuteResult<>();

        JPAQuery<Subjects> subjectsJPAQuery = this.subjectsRepository.selectFrom(qSubjects).where(qSubjects.id.eq(subjectId));
        if(0!=subjectsJPAQuery.fetchCount()){
            Subjects subjects =subjectsJPAQuery.fetchFirst();

            if(0==subjects.getQuestions().size()&&0==subjects.getTemplateQuestions().size()&&0==subjects.getUserAnswerSheetQuestions().size()){
                this.subjectsRepository.remove(subjects);

                servicesExcuteResult.setSuccess(true);
            }
            else{
                servicesExcuteResult.setErrorMessage("该科目不可以删除");
            }
        }
        else{
            throw new ResourceNotFoundException("科目不存在");
        }
        return servicesExcuteResult;
    }

    @Override
    public ServicesExcuteResult<PageResponse<SubjectTransfer>> querySubjects(@Valid QuerySubjectRequest querySubjectRequest) {
        ServicesExcuteResult<PageResponse<SubjectTransfer>> servicesExcuteResult = new ServicesExcuteResult<>();

        JPAQuery<Subjects> subjectsJPAQuery = this.subjectsRepository.selectFrom(qSubjects);
        if(!StringUtils.isEmpty(querySubjectRequest.getName())){
            subjectsJPAQuery = subjectsJPAQuery.where(qSubjects.name.eq(querySubjectRequest.getName()));
        }
        long totalCount = subjectsJPAQuery.fetchCount();

        subjectsJPAQuery =subjectsJPAQuery.orderBy(qSubjects.id.asc())
                                .offset((querySubjectRequest.getPageIndex()-1)*querySubjectRequest.getPageSize()).limit(querySubjectRequest.getPageSize());

        List<Subjects> subjects = subjectsJPAQuery.fetch();

        List<SubjectTransfer> subjectTransfers= subjectMapper.mapFrom(subjects);

        PageResponse<SubjectTransfer> subjectTransferPageResponse = new PageResponse<>(querySubjectRequest.getPageSize(),querySubjectRequest.getPageIndex(),(int)totalCount,subjectTransfers);

        servicesExcuteResult.setData(subjectTransferPageResponse);
        servicesExcuteResult.setSuccess(true);
        return servicesExcuteResult;
    }
}
